using Identity.Application.RolePermissions.Interfaces;
using Identity.Domain.Aggregates;

namespace Identity.Infrastructure.Persistence.ReadServices;

public class RolePermissionReadService : IRolePermissionsReadService
{
    private readonly ApplicationDbContext _context;
    public RolePermissionReadService(ApplicationDbContext context)
    {
        _context = context;
    }
    public async Task<List<RolePermission>> GetRolePermissions()
    {
        var roles = _context.RolePermissions
        .Select(r => new RolePermission(r.RoleId, Role.LoadFromPersistence(r.Role.Id, r.Role.Name!,r.Role.NormalizedName,r.Role.RolePermissions
        .Select(rp=>rp.PermissionId), r.Role.Description,r.Role.Status), r.PermissionId, r.Permission));
        
        return roles.ToList();
    }
}